<?php
class Offline extends Vfriend_Db {	
	var $cache;
	var $clear;
	var $notify;
	var $behaviour;
	var $mlogs;
	public function __construct() {
		$this->cache 		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;
		$this->notify 		= new Notification();
		$this->behaviour	= new Behaviour();
		$this->mlogs		= new Mlogs();
	}
	public function add($intUserId, $intCategoryId, $strTitle, $strContent, $strShort, 
						$intQuantity, $strCountry, $intProvince, $intDistrict, $strAddr, 
						$intSrcId, $strExpired, $strMobile='', $strYahoo='', $strSkype='',&$intOfflineId=0) {
		try {				
			$this->prepare('call offlines_add(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $strTitle, PDO::PARAM_STR, 255);			
			$this->bindParam(4, $strContent, PDO::PARAM_STR);			
			$this->bindParam(5, $strShort, PDO::PARAM_STR, 255);			
			$this->bindParam(6, $intQuantity, PDO::PARAM_INT, 3);			
			$this->bindParam(7, $strCountry, PDO::PARAM_STR, 2);			
			$this->bindParam(8, $intProvince, PDO::PARAM_INT, 11);			
			$this->bindParam(9, $intDistrict, PDO::PARAM_INT, 11);			
			$this->bindParam(10, $strAddr, PDO::PARAM_STR, 255);			
			$this->bindParam(11, $intSrcId, PDO::PARAM_INT, 11);			
			$this->bindParam(12, $strExpired, PDO::PARAM_STR, 20);						
			$this->bindParam(13, $strMobile, PDO::PARAM_STR, 20);						
			$this->bindParam(14, $strYahoo, PDO::PARAM_STR, 150);						
			$this->bindParam(15, $strSkype, PDO::PARAM_STR, 150);						
			$arrResult 		= $this->fetchAll();	
			$intError		= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;			
			if($intError == 0) {
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByCategory($intCategoryId);
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteUstat($intUserId);
				$intOfflineId  	= isset($arrResult[0]['offlineid']) ? $arrResult[0]['offlineid'] : 0;
				$strContentJson = Zend_Json::encode(array('posterid'=>$intUserId,'offlineid'=>$intOfflineId,'title'=>$strTitle));
				$objVfriendUser = new Vfriend_User();
				$arrRelation	= $objVfriendUser->getRelation($intUserId,2);
				if($arrRelation) {					
					foreach($arrRelation as $value) {	
						$this->mlogs->insert($value,60,$strContentJson);
						$this->notify->insert($value,53,$strContentJson,$intOfflineId);
						$this->logBehaviour($value,$intUserId,$intUserId,41,$intOfflineId);
					}
				}
				$this->logBehaviour($intUserId,$intUserId,$intUserId,41,$intOfflineId);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::add - ' .$e->getMessage());			
			return null;
		}
	}	
	public function addQuick($intUserId, $intCategoryId, $strTitle, $intProvince, $strMobile,&$intOfflineId=0) {
		try {			
			$this->prepare('call offlines_add_quick(?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intCategoryId, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $strTitle, PDO::PARAM_STR, 255);											
			$this->bindParam(4, $intProvince, PDO::PARAM_INT, 11);											
			$this->bindParam(5, $strMobile, PDO::PARAM_STR, 20);											
			$arrResult 	= $this->fetchAll();			
			$intError	= isset($arrResult[0]['offlineid']) ? $arrResult[0]['offlineid'] : 0;
			if($intError) {	
				$intOfflineId  = isset($arrResult[0]['offlineid']) ? $arrResult[0]['offlineid'] : 0;
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByCategory($intCategoryId);
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteUstat($intUserId);
				$strContentJson = Zend_Json::encode(array('posterid'=>$intUserId,'offlineid'=>$intOfflineId,'title'=>$strTitle));
				$objVfriendUser = new Vfriend_User();
				$arrRelation	= $objVfriendUser->getRelation($intUserId,2);
				if($arrRelation) {					
					foreach($arrRelation as $value) {	
						$this->mlogs->insert($value,60,$strContentJson);
						$this->notify->insert($value,53,$strContentJson,$intOfflineId);
						$this->logBehaviour($value,$intUserId,$intUserId,41,$intOfflineId);
					}
				}
				$this->logBehaviour($intUserId,$intUserId,$intUserId,41,$intOfflineId);	
			}
			return $$intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::addQuick - ' .$e->getMessage());			
			return null;
		}
	}	
	public function update($intUserId, $intOfflineId, $intCategoryId, $strTitle, $strContent, $strShort, 
						$intQuantity, $strCountry, $intProvince, $intDistrict, $strAddr, 
						$intSrcId, $strExpired, $strMobile='', $strYahoo='', $strSkype='') {
		try {						
			$this->prepare('call offlines_update(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);			
			$this->bindParam(3, $intCategoryId, PDO::PARAM_INT, 11);			
			$this->bindParam(4, $strTitle, PDO::PARAM_STR, 255);			
			$this->bindParam(5, $strContent, PDO::PARAM_STR);			
			$this->bindParam(6, $strShort, PDO::PARAM_STR, 255);			
			$this->bindParam(7, $intQuantity, PDO::PARAM_INT, 3);			
			$this->bindParam(8, $strCountry, PDO::PARAM_STR, 2);			
			$this->bindParam(9, $intProvince, PDO::PARAM_INT, 11);			
			$this->bindParam(10, $intDistrict, PDO::PARAM_INT, 11);			
			$this->bindParam(11, $strAddr, PDO::PARAM_STR, 255);			
			$this->bindParam(12, $intSrcId, PDO::PARAM_INT, 11);			
			$this->bindParam(13, $strExpired, PDO::PARAM_STR, 20);						
			$this->bindParam(14, $strMobile, PDO::PARAM_STR, 20);						
			$this->bindParam(15, $strYahoo, PDO::PARAM_STR, 150);						
			$this->bindParam(16, $strSkype, PDO::PARAM_STR, 150);						
			$arrResult 		= $this->fetchAll();	
			$intError		= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;	
			if($intError == 0) {		
				$intOldCateId  	= isset($arrResult[0]['categoryid']) ? $arrResult[0]['categoryid'] : 0;
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				if($intOldCateId != $intCategoryId) {
					$this->clear->deleteOfflineByCategory($intOldCateId);
					$this->clear->deleteOfflineByCategory($intCategoryId);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::update - ' .$e->getMessage());			
			return null;
		}
	}	
	public function updateStatus($intUserId, $intOfflineId) {
		try {			
			$this->prepare('call offlines_update_status(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);			
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);											
			$arrResult 	= $this->fetchAll();			
			$intError  = isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {	
				$intCategoryId = isset($arrResult[0]['categoryid']) ? $arrResult[0]['categoryid'] : 0;
				$strTitle = isset($arrResult[0]['title']) ? $arrResult[0]['title'] : 0;
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByCategory($intCategoryId);
				$this->clear->deleteUstat($intUserId);
				$arrJoin = $this->getAllForOjoin($intOfflineId,4,0,100);
				if($arrJoin) {
					$strContentJson = Zend_Json::encode(array('posterid'=>$intUserId,'offlineid'=>$intOfflineId,'title'=>$strTitle));
					foreach($arrJoin as $value) {
						$intReceiveId = $value['userid'];
						$this->notify->insert($intReceiveId,59,$strContentJson,$intOfflineId);
						$this->mlogs->insert($intReceiveId,66,$strContentJson);
					}
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::updateStatus - ' .$e->getMessage());			
			return null;
		}
	}	
	public function get($intOfflineId) {
		try {
			$arrResult	= null;
			$strKey 	= OFFLINE_GET . $intOfflineId;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_get(?)');
				$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);											
				$arrResult 	= $this->fetchAll();	
				$this->cache->set($strKey, $arrResult, OFFLINE_GET_EXPIRE);
			}						
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getRss() {
		try {
			$strKey 	= OFFLINE_GETRSS;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_getall_rss()');
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, OFFLINE_GETRSS_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('News::getRss - ' .$e->getMessage());			
			return null;
		}
	}
	public function getOjoin($intUserId, $intOfflineId) {
		try {
			$arrResult	= null;
			$strKey 	= OFFLINE_GETOJOIN . $intUserId . $intOfflineId;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ojoins_get(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);											
				$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);											
				$arrResult 	= $this->fetchAll();	
				$this->cache->set($strKey, $arrResult, OFFLINE_GETOJOIN_EXPIRE);
			}						
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getOjoin - ' .$e->getMessage());			
			return null;
		}
	}
	
	public function getAll($intUserId, $strOfflineId, $intOffset, $intLimit,&$intTotal=0) {
		try {
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= OFFLINE_GETALL . $intUserId . $intLimit;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_getall(?,?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);				
				$this->bindParam(2, $strOfflineId, PDO::PARAM_STR);				
				$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);			
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);											
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, OFFLINE_GETALL_EXPIRE);
				}
			}			
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForMatch($intUserId, $intOffset, $intLimit,&$intTotal=0) {
		try {
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= OFFLINE_GETALLFORMATCH . $intUserId . $intLimit;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_getall_match(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);				
				$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);			
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);											
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, OFFLINE_GETALLFORMATCH_EXPIRE);
				}
			}			
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getAllForMatch - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForCategory($intCategoryId, $intOffset, $intLimit,&$intTotal=0) {
		try {
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= OFFLINE_GETALLFORCATEGORY . $intCategoryId;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_getall_category(?,?,?)');
				$this->bindParam(1, $intCategoryId, PDO::PARAM_INT, 11);				
				$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);			
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);											
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, OFFLINE_GETALLFORCATEGORY_EXPIRE);
				}
			}			
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getAllForCategory - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForMine($intUserId, $intType, $intOffset, $intLimit,&$intTotal=0) {
		try {
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= OFFLINE_GETALLFORMINE . $intUserId . $intType . $intLimit;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_getall_mine(?,?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);				
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);				
				$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);			
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);											
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, OFFLINE_GETALLFORMINE_EXPIRE);
				}
			}			
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getAllForMine - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForExpire($intUserId, $intOffset, $intLimit,&$intTotal=0) {
		try {
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= OFFLINE_GETALLFOREXPIRE . $intUserId;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_getall_expired(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);			
				$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);			
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);											
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, OFFLINE_GETALLFOREXPIRE_EXPIRE);
				}
			}			
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getAllForExpire - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAllForOjoin($intOfflineId, $intStatus, $intOff, $intLimit,&$intTotal=0,$intTyCache=0) {
		try {
			$arrResult	= null;
			if($intOff == 0 && $intTyCache == 0) {
				$strKey 	= OFFLINE_GETALLFOROJOIN . $intOfflineId . $intStatus;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ojoins_getall(?,?,?,?)');
				$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);											
				$this->bindParam(2, $intStatus, PDO::PARAM_INT, 4);											
				$this->bindParam(3, $intOff, PDO::PARAM_INT, 11);											
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);											
				$arrResult 	= $this->fetchAll();	
				if($intOff == 0 && $intTyCache == 0) {
					$this->cache->set($strKey, $arrResult, OFFLINE_GETALLFOROJOIN_EXPIRE);
				}
			}						
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getAllForOjoin - ' .$e->getMessage());			
			return null;
		}
	}
	public function listFriendInvite($intUserId, $intOfflineId)  {
		try {	
			$strKey	   = OFFLINE_LISTFRIENDINVITE . $intOfflineId;
			$arrResult = $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call urelations_getall_offlines(?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult,OFFLINE_LISTFRIENDINVITE_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::listFriendInvite - ' .$e->getMessage());			
			return null;
		}
	}
	public function addCmt($intUserId, $intOfflineId, $strContent,&$intOwnerId=0) {
		try {
			$this->prepare('call ocomments_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strContent);
			$arrResult	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
			if($intError == 0) {	
				$strTitle	= isset($arrResult[0]['title']) ? $arrResult[0]['title'] : 0;
				$intCmtId	= isset($arrResult[0]['ocommentid']) ? $arrResult[0]['ocommentid'] : 0;	
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteOfflineById($intOfflineId, $intOwnerId);
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByUserId($intOwnerId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intOwnerId);
				$isHasOwner = 0;
				$strContentJson = Zend_Json::encode(
													array(	'posterid'=>$intUserId,
															'ownerid'=>$intOwnerId,
															'offlineid'=>$intOfflineId,
															'title'=>$strTitle,
															'cmt'=>$strContent
														)
												);
				// BEHAVIOUR
				$arrUser	= $this->getCmtUser($intOfflineId);
				if($arrUser) {									
					foreach($arrUser as $value) {
						$intReceiveId = $value['userid'];
						if($intReceiveId == $intOwnerId) {
							$isHasOwner++;
						}
						if($intUserId != $intReceiveId) {
							$this->mlogs->insert($value,61,$strContentJson);
							$this->notify->insert($intReceiveId,54,$strContentJson,$intCmtId);
						}
						$this->logBehaviour($intReceiveId, $intOwnerId, $intUserId, 42, $intOfflineId);						
					}
					if(!$isHasOwner && $intOwnerId != $intUserId) {
						$this->mlogs->insert($intOwnerId,61,$strContentJson);
						$this->notify->insert($intOwnerId,54,$strContentJson,$intCmtId);
					}					
				}
				$this->logBehaviour($intOwnerId, $intOwnerId, $intUserId, 42, $intOfflineId);
				$this->logBehaviour($intUserId, $intOwnerId, $intUserId, 42, $intOfflineId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::addCmt - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtUser($intOfflineId) {
		try {	
			$strKey	 	= OFFLINE_GETCMTUSER . $intOfflineId;
			$arrResult 	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ocomments_get_user(?)');
				$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, OFFLINE_GETCMTUSER_EXPIRE);
			}
			return $arrResult;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getCmtUser - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmtTotal($intOfflineId) {
		try {	
			$strKey	   = OFFLINE_GETCMTTOTAL . $intOfflineId;
			$arrResult = $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ocomments_get_total(?)');
				$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult,OFFLINE_GETCMTTOTAL_EXPIRE);
			}
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return $intTotal;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getCmtTotal - ' .$e->getMessage());			
			return null;
		}
	}
	public function getCmt($intOfflineId, $intOffset, $intLimit) {
		try {	
			$this->prepare('call ocomments_getall(?,?,?)');
			$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intOffset, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);	
			$arrResult 	= $this->fetchAll();
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getCmtList - ' .$e->getMessage());			
			return null;
		}
	}
	public function getTopCmt($intOfflineId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {	
			$strKey	   = OFFLINE_GETTOPCMT . $intOfflineId;
			$arrResult = $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call ocomments_get_top(?,?)');
				$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();	
				$this->cache->set($strKey, $arrResult,OFFLINE_GETTOPCMT_EXPIRE);				
			}
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getCmtList - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteCmt($intUserId, $intOfflineId, $intOcommentId) {
		try {
			$this->prepare('call ocomments_delete(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOcommentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intOwnerId	= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteOfflineById($intOfflineId, $intOwnerId);
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByUserId($intOwnerId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				$this->clear->deleteUstat($intUserId);
				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::deleteCmt - ' .$e->getMessage());			
			return null;
		}
	}
	public function addLikeCmt($intUserId, $intOfflineId, $intOcommentId, &$intLike=0) {
		try {
			$this->prepare('call oclikes_add(?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOcommentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$intLike 		= isset($arrResult[0]['tlike']) ? $arrResult[0]['tlike'] : 0;
				$intOwnerId		= isset($arrResult[0]['userid']) ? $arrResult[0]['userid'] : 0;
				$strTitle 		= isset($arrResult[0]['title']) ? $arrResult[0]['title'] : '';
				$strContent 	= isset($arrResult[0]['content']) ? $arrResult[0]['content'] : '';
				$intCmtorId 	= isset($arrResult[0]['cuserid']) ? $arrResult[0]['cuserid'] : 0;
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				$this->clear->deleteUstat($intUserId);
				// notify
				$strContentJson = Zend_Json::encode(
														array(	'posterid'=>$intUserId,
																'ownerid'=>$intOwnerId,
																'offlineid'=>$intOfflineId,
																'title'=>$strTitle,
																'cmt'=>$strContent
															)
													);
				$this->notify->insert($intCmtorId,62,$strContentJson,$intOcommentId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::addLikeCmt - ' . $e->getMessage());
			return null;
		}
	}	
	public function ojoinReg($intUserId, $intOfflineId, $intOwnerId, $intStatus=0) {
		try {
			$this->prepare('call ojoins_add(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOwnerId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intStatus, PDO::PARAM_INT, 4);			
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strTitle 	= isset($arrResult[0]['title']) ? $arrResult[0]['title'] : 0;
				$intOwnerId = isset($arrResult[0]['ownerid']) ? $arrResult[0]['ownerid'] : 0;
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteOfflineById($intOfflineId, $intOwnerId);
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByUserId($intOwnerId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intOwnerId);
				if($intStatus==-1) {
					$intRefObjId = isset($arrResult[0]['ojoinid']) ? $arrResult[0]['ojoinid'] : 0;
					
					$objURequest = new URequest();
					$strContent	 = Zend_Json::encode(array(	'offlineid'=>$intOfflineId,
															'title'=>$strTitle,
															'posterid'=>$intOwnerId
															)
														);
					$objURequest->addRequest($intUserId, $intOwnerId, $strContent, $intRefObjId, 9);					
					$this->notify->insert($intUserId,56,$strContent,$intOfflineId);
					$this->mlogs->insert($intUserId,63,$strContent);
				} else {
					$strContentJson = Zend_Json::encode(
													array(	'posterid'=>$intUserId,
															'offlineid'=>$intOfflineId,
															'title'=>$strTitle
														)
												);
					$this->mlogs->insert($intOwnerId,62,$strContentJson);
					$this->notify->insert($intOwnerId,55,$strContentJson,$intOfflineId);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::ojoinReg - ' . $e->getMessage());
			return null;
		}
	}
	public function ojoinCancelation($intUserId, $intOfflineId) {
		try {
			$this->prepare('call ojoins_cancelation(?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOfflineId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {	
				$intOwnerId = isset($arrResult[0]['ownerid']) ? $arrResult[0]['ownerid'] : 0;
				$strTitle 	= isset($arrResult[0]['title']) ? $arrResult[0]['title'] : 0;
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				$this->clear->deleteUstat($intUserId);
				$strContentJson = Zend_Json::encode(array('posterid'=>$intUserId,'offlineid'=>$intOfflineId,'title'=>$strTitle));
				$this->notify->insert($intOwnerId,60,$strContentJson,$intOfflineId);
				$this->mlogs->insert($intOwnerId,67,$strContentJson);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::ojoinReg - ' . $e->getMessage());
			return null;
		}
	}
	public function ojoinUpdateStatus($intUserId, $intOjoinId, $intOfflineId, $intOwnerId, $intStatus, $intType=1) {
		try {			
			if($intType == 1) $intOwId = 0;
			else $intOwId = $intOwnerId;
			$this->prepare('call ojoins_update_status(?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intOjoinId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOfflineId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intOwId, PDO::PARAM_INT, 11);
			$this->bindParam(5, $intStatus, PDO::PARAM_INT, 4);			
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intOwnerId = isset($arrResult[0]['ownerid']) ? $arrResult[0]['ownerid'] : 0;
				$strTitle 	= isset($arrResult[0]['title']) ? $arrResult[0]['title'] : '';
				$intOjoinId = isset($arrResult[0]['ojoinid']) ? $arrResult[0]['ojoinid'] : 0;
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);
				$this->clear->deleteOfflineById($intOfflineId, $intOwnerId);
				$this->clear->deleteOfflineByUserId($intUserId);
				$this->clear->deleteOfflineByUserId($intOwnerId);
				$this->clear->deleteCmtByOfflineId($intOfflineId);
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intOwnerId);
				if($intType == 1) {
					if($intStatus == 1) {
						$intType 	= 57;
						$intEtype 	= 64;
					} elseif($intStatus == -2) {
						$intType 	= -57;
						$intEtype	= -64;
					}
					$objURequest = new URequest();
					$objURequest->deleteRequestByRefObjId($intOjoinId,$intUserId,$intOwnerId,9);
					$strContentJson = Zend_Json::encode(
													array(	'posterid'=>$intUserId,
															'offlineid'=>$intOfflineId,
															'title'=>$strTitle
														)
												);
					$this->notify->insert($intOwnerId,$intType,$strContentJson,$intOfflineId);
					$this->mlogs->insert($intOwnerId,$intEtype,$strContentJson);
				} else {
					if($intStatus == 1) {
						$intType 	= 58;
						$intEtype 	= 65;
					} elseif($intStatus == 2) {
						$intType 	= -58;
						$intEtype	= -65;
					}
					$strContentJson = Zend_Json::encode(
													array(	'posterid'=>$intOwnerId,
															'offlineid'=>$intOfflineId,
															'title'=>$strTitle
														)
												);
					$this->notify->insert($intUserId,$intType,$strContentJson,$intOfflineId);
					$this->mlogs->insert($intOwnerId,$intEtype,$strContentJson);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::ojoinUpdateStatus - ' . $e->getMessage());
			return null;
		}
	}
	public function orateAdd($intUserId, $intTargetId, $intOfflineId, $intPoint) {
		try {						
			$this->prepare('call orates_add(?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intTargetId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOfflineId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $intPoint, PDO::PARAM_INT, 4);		
			$arrResult 	= $this->fetchAll();
			$intError 	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deleteOfflineById($intOfflineId, $intUserId);				
				$this->clear->deleteOfflineById($intOfflineId, $intTargetId);				
				$this->clear->deleteOfflineByUserId($intUserId);				
				$this->clear->deleteOfflineByUserId($intTargetId);	
				$this->clear->deleteCmtByOfflineId($intOfflineId);	
				$this->clear->deleteUstat($intUserId);
				$this->clear->deleteUstat($intTargetId);				
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::orateAdd - ' . $e->getMessage());
			return null;
		}
	}
	public function getQuote($intOfflineId, $intType) {
		try {
			$strKey	 	= OFFLINE_GETQUOTE . $intOfflineId . $intType;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call offlines_get_quote(?,?)');
				$this->bindParam(1, $intOfflineId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intType, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, OFFLINE_GETQUOTE_EXPIRE);
			}
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getQuote - ' . $e->getMessage());
			return null;
		}
	}
	public function getLike($intOfflineId, $intLimit=BEHAVIOUR_TOP_LIMIT) {
		try {
			/*$strKey	 	= NEWS_GETLIKE . $intNewId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call nlikes_get_top(?,?)');
				$this->bindParam(1, $intNewId, PDO::PARAM_INT, 11);	
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();
				$this->cache->set($strKey, $arrResult, NEWS_GETLIKE_EXPIRE);
			}
			return isset($arrResult) ? $arrResult : null;
			*/
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Offline::getLike - ' . $e->getMessage());
			return null;
		}
	}
	public function logBehaviour($intUserId,$intOwnerId,$intPosterId,$intType,$intSrcId) {
		$strQueryCore 		= 'get';
		$strQueryLike		= 'getLike';
		$strQueryTotalCmt	= 'getCmtTotal';
		$strQueryCmt		= 'getTopCmt';		
		$strQueryQuote		= 'getQuote';
		$this->behaviour->insert($intUserId,
								 $intOwnerId,
								 $intPosterId,
								 $strQueryCore,
								 $strQueryLike,
								 $strQueryTotalCmt,
								 $strQueryCmt,
								 $strQueryQuote,
								 $intType,
								 $intSrcId);
	}
}
?>